package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.dto.BaseDto;
import com.entity.Goods;
import com.entity.Orderinfo;
import com.page.PageInfo;
import com.service.OrderinfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;

@Controller
public class OrderinfoController {
    @Resource
    OrderinfoService orderinfoService;

//传统传参，不需要绑定参数 url直接在后面写上？adid=0001
    @RequestMapping("/orderinfo/getByAdidJson")
    @ResponseBody
    public Orderinfo getByAdidJson(String orid) {
        return orderinfoService.getByOrid(orid);
    }

//rest风格传参 将参数变为url的一部分但是需要绑定 例如 /orderinfo/getByAdidNew/0001
    @RequestMapping("/orderinfo/getByAdidNewJSon/{orid}")
    @ResponseBody
    public Orderinfo getByAdidNewJson(@PathVariable("orid") String orid) {
        return orderinfoService.getByOrid(orid);
    }

    @RequestMapping("/orderinfo/getAllJson")
    @ResponseBody
    public BaseDto<Orderinfo> getAllJson() {
        return orderinfoService.getAll();

    }

    @RequestMapping("/orderinfo/getAll")
    public ModelAndView getAll() {
       BaseDto<Orderinfo> dto = orderinfoService.getAll();
        return new ModelAndView("/back/orderinfo/list","dto",dto);

    }

    @RequestMapping("/orderinfo/getAllByPage")
    public ModelAndView getAllByPage(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Orderinfo> dto = orderinfoService.getAllByPage(pageInfo);
        ModelAndView mv = new ModelAndView("/back/orderinfo/list","dto",dto);
        return mv;
    }
    @RequestMapping("/orderinfo/getAllByPageJson")
    @ResponseBody
    public BaseDto getAllByPageJson(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Orderinfo> dto = orderinfoService.getAllByPage(pageInfo);
        return dto;
    }


    @RequestMapping("/orderinfo/searchNameByPage/{adname}")
    @ResponseBody
    public BaseDto searchNameByPage(PageInfo pageInfo,@PathVariable("adname")String adname) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Orderinfo> dto = orderinfoService.searchNameByPage(pageInfo,adname);
        return dto;
    }


    /**
     * 根据主键查询
     * @param orid
     * @return
     */
    @RequestMapping("/orderinfo/getOrderinfoByAdid/{orid}")
    @ResponseBody
    public BaseDto getOrderinfoByAdid(@PathVariable("orid") String orid){
        return orderinfoService.getOrderinfoByOrid(orid);
    }

    /**
     * 添加一个实体
     * @param orderinfo
     * @return
     */
    @RequestMapping("/orderinfo/addOrderinfo")
    @ResponseBody
    public BaseDto addOrderinfo(Orderinfo orderinfo){
        return orderinfoService.addOrderinfo(orderinfo);
    }
    /**
     * 修改实体
     */
    @RequestMapping("/orderinfo/updateOrderinfo")
    @ResponseBody
    public BaseDto updateOrderinfo(Orderinfo orderinfo){
        return orderinfoService.updateOrderinfo(orderinfo);
    }
    public BaseDto updateOrderinfoByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param orid
     * @return
     */
    @RequestMapping("/orderinfo/deleteOrderinfoByPrimayKey/{orid}")
    @ResponseBody
    public BaseDto deleteOrderinfoByPrimayKey(@PathVariable("orid") String orid){
        return orderinfoService.deleteOrderinfoByPrimayKey(orid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteOrderinfoByExample(){
        return null;
    }

    //保存到cookie 购物车
    @PostMapping("/orderinfo/saveGoodsCar")
    @ResponseBody
    public BaseDto saveGoodsCar(@RequestBody String[] gids) {
        return orderinfoService.getByGtids(Arrays.asList(gids));
    }


    @GetMapping("/orderinfo/initGoodsCar")
    @ResponseBody
    public BaseDto initGoodsCar() {
        return orderinfoService.initGoodsCarFromCookie();
    }


    /**
     * 生成订单到redis
     */
    @PostMapping("/orderinfo/addOrderinfoByGtids")
    @ResponseBody
    public  BaseDto addOrderinfoByGtids(@RequestBody String[] gids) {
       return  orderinfoService.addOrderinfoByGtids(Arrays.asList(gids));
    }

    /**
     * 完成支付
     */

    @RequestMapping("/orderinfo/pay/{orid}")
    @ResponseBody
   public BaseDto pay(@PathVariable("orid")String orid){
    return orderinfoService.pay(orid);
    }

}
